import { _decorator, Camera, Component, instantiate, Node, Prefab, random, Vec3 } from 'cc';
import { GameState } from './GameState';
import { UIMain } from '../UI/UIMain';
import { UIRoot } from '../../ISUtility/Script/UI/UIRoot';
import { UIGaming } from '../UI/UIGaming';
import { GameUnit } from './Unit/GameUnit';
import { Slime } from './Unit/Slime';
import { UnitMgr } from './UnitMgr';
import { Sound } from '../../ISUtility/Script/Sound';
import { CameraCtrl } from './CameraCtrl';
import { GameConfig } from './GameConfig/GameConfig';
import { LevelConfig } from './GameConfig/LevelConfig';
import { ResMgr } from '../../ISUtility/Script/ResMgr';
import { GameLevelBase } from './Level/LevelBase';
import { Tools } from '../../ISUtility/Script/Tools';
const { ccclass, property } = _decorator;

@ccclass('GameWorld')
export class GameWorld extends Component {
    private static _inst: GameWorld;
    public static get inst(): GameWorld {
        return GameWorld._inst;
    }


    @property({ type: Camera })
    public Camera: Camera = null;

    @property({ type: Camera })
    public ReadyCamera: Camera = null;

    @property({ type: Node })
    public CameraAnchor: Node = null;

    public get State() { return this._state; }
    private _state: GameState = GameState.None;

    public get Slime() {
        if (null == this._slime)
            this._slime = UnitMgr.inst.FindUnit(Slime);
        return this._slime;
    }
    private _slime: Slime;

    public get Level() {
        return this._level;
    }
    private _level : GameLevelBase  = null;

    protected onLoad(): void {
        GameWorld._inst = this;
        let config = GameConfig.inst;
    }

    start() {
        this.ReadyGame();
    }

    update(deltaTime: number) {

    }

    public ReadyGame() {

        this.Camera.node.active = false;
        this.ReadyCamera.node.active = true;

        UIRoot.inst.OpenUI(UIMain)
        Sound.inst.SetBGM("Bundle/Sound/a_welcome_hauntingOGG");

        GameConfig.inst.Get(LevelConfig,(cfg)=>{
            let levCfg = cfg.Find(0);
            ResMgr.inst.load("Bundle/Level/" + levCfg.LevelPrefab, Prefab, null, (err, prefab) => {
                let levelNode = instantiate(prefab);
                this.node.addChild(levelNode);
                this._level = levelNode.getComponent(GameLevelBase);
                
                UnitMgr.inst.Create("Bundle/GameUnit/Slime/Slime01", Slime, (unit) => {
                    unit.node.position = Vec3.ZERO;
                });
            });
        });

    }

    public StartGame() {
        console.log("游戏开始")

        this.Camera.node.active = true;
        this.ReadyCamera.node.active = false;

        this.Camera.node.setPosition(this.ReadyCamera.node.position);
        this.Camera.node.eulerAngles = this.ReadyCamera.node.eulerAngles;

        UIRoot.inst.HideUI(UIMain);
        this.Camera.getComponent(CameraCtrl).TweenTo(this.CameraAnchor, 0.3, () => {
            UIRoot.inst.CloseUI(UIMain);
            UIRoot.inst.OpenUI(UIGaming);
            GameWorld.inst.Slime.node.addChild(this.Camera.node);
        });

        Sound.inst.SetBGM("Bundle/Sound/cute pack " + Tools.RangeInt(1,4));

        this._level.OnGameStart();
    }
}


